package com.itheima.health.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.mapper.OrderMapper;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisMessageConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.Map;


/**
 * @author 十一
 * @Description
 * @create 2020-11-06-18:53
 */
@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Reference
    private OrderSettingService orderSettingService;

    @Reference
    private MemberService memberService;

    //提交用户预约信息
    @Override
    @LcnTransaction
    public Order add(Map map) {
        //获取手机号，验证码
        String  codeFromMap = (String)map.get("validateCode");
        String  telephone = (String)map.get("telephone");
        String redisCode  = RedisUtil.get(RedisMessageConstant.SENDTYPE_ORDER +telephone);
        //1、校验手机验证码是否过期
        if(StringUtils.isBlank(redisCode)){
             throw new RuntimeException("验证码已过期");
        }
        //2、判断用户是否输入验证码
        if(StringUtils.isBlank(codeFromMap)){
            throw new RuntimeException("请输入验证码");
        }
        //3、判断用户输入的验证码是否正确
        if(! redisCode.equals(codeFromMap)){
            throw new RuntimeException("验证码错误");
        }
        //4、判断当前时期是否还能预约
        //获取当前日期
        String  orderDate = (String) map.get("orderDate");
        int count = orderSettingService.isOrderOK(orderDate);
        if(count <= 0){
            throw new RuntimeException("当前日期预约人数已满，请选择其他日期");
        }
        //5、判断当前用户是否是新用户和重复预约 orderDate、member_id、SetMeal_id
        String sid = (String) map.get("setmealId");
        //查询会员表判断是否存在当前用户
        Member member = memberService.findMemberByTelephone(telephone);
        int mid = 0;
        if(member != null){
            //老用户——判断是否重复预约
            int orderExist = baseMapper.findOrderBySetMealIdAndOrderDateAndMemberId(Integer.parseInt(sid),orderDate,member.getId());
            mid =member.getId();
            if(orderExist > 0){
                throw  new RuntimeException("该日期您已预约，请勿重复预约");
            }
        }else{
            //新用户 - 插入新用户数据
         member = new Member();
         member.setName((String)map.get("name"));
         member.setSex((String)map.get("sex"));
         member.setIdCard((String)map.get("idCard"));
         member.setPhoneNumber(telephone);
         member.setRegTime(new Date(System.currentTimeMillis()));
         mid = memberService.saveMember(member);
        }
        //预约成功，更新预约表数据
        orderSettingService.updateReservationByOrderDate(orderDate);
        //更新订单表数据
        Order  order = new  Order();
        order.setMemberId(mid);
        order.setOrderDate(DateUtils.parseString2Date(orderDate,"yyyy-MM-dd"));
        order.setOrderType((String)map.get("orderType"));
        order.setSetmealId(Integer.parseInt(sid));
        order.setOrderStatus("未到诊");
        save(order);
        return order;
    }

    @Override
    public Map findOrderInfoByOrderId(int id) {
        Map map = baseMapper.findOrderInfoByOrderId(id);
        String orderDate = String.valueOf(map.get("orderDate"));
        map.replace("orderDate",orderDate);
        return map;
    }
}
